<template>
  <jy-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" highlight-current @node-click="handleNodeClick">
            <template slot-scope="{ node, data }">
              <slot v-bind="{ node, data }">
                <span :title="node.label" class="el-tree-node__label">
                  {{ node.label }}
                </span>
              </slot>
            </template>
          </el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
          @submit.native.prevent>
          <el-form-item label="姓名" prop="nickName">
            <el-input v-model="queryParams.nickName" placeholder="请输入姓名" @keyup.enter.native="handleQuery" clearable
              style="width: 240px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
              @keyup.space.native.prevent="() => { }">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              @keyup.space.native.prevent="() => { }">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table border ref="table" v-loading="loading" :data="userList" max-height="500" highlight-current-row
          @current-change="handleCurrentChange">
          <el-table-column width="50" label="选择" class-name="radio-border">
            <template slot-scope="scope">
              <el-radio :label="scope.row.userId" v-model="currentUserId">{{ '' }}</el-radio>
            </template>
          </el-table-column>
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <!-- <el-table-column label="用户编号" align="center" key="userId" prop="userId" /> -->
          <!-- <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" /> -->
          <el-table-column label="用户姓名" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName"
            :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120" />
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" :disabled="!(currentRow && currentRow.userId)">确 定</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </jy-dialog>
</template>

<script>
import { listWithNoPermission, deptTreeWithNoPermission } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AddressBook",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { Treeselect },
  data () {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "企业通讯录",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        deptId: undefined,
        status: 0
      },
      code: undefined,
      currentRow: {},
      currentUserId: undefined,
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.handleQuery();
    this.getDeptTree();
  },
  methods: {
    init (code) {
      this.resetQuery();
      this.code = code;
      this.open = true;
    },
    /** 查询用户列表 */
    getList () {
      this.loading = true;
      listWithNoPermission(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree () {
      deptTreeWithNoPermission().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.deptName = undefined;
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        deptId: undefined,
        status: 0
      };
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null);
      }
      if (this.$refs.table) {
        this.$refs.table.setCurrentRow();
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleCurrentChange (val) {
      this.currentRow = val;
      this.currentUserId = val?.userId;
    },
    // 取消按钮
    cancel () {
      this.resetQuery();
      this.open = false;
    },

    submit () {
      const info = {
        ...this.currentRow,
        code: this.code
      };
      this.$emit('callBack', info);
      this.cancel();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .radio-border .cell {
  display: flex;
  justify-content: center;
  padding-right: 0;
}
</style>
